package cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree;

import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.DataCatalog;
import cn.edu.cug.cs.gtl.protos.DataGroup;
import cn.edu.cug.cs.gtl.protos.DataItem;
import cn.edu.cug.cs.gtl.utils.postgresql.DataCatalogManager4Postgresql;
import cn.edu.cug.cs.gtl.utils.postgresql.DataGroupManager4Postgresql;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class JTreeTools {

    public static void expandGroup(JTree tree, DefaultMutableTreeNode parentNode, Object object) {
        if (object instanceof DataGroup) {
            //将数据组节点添加到树中
            DataGroup childGroup = (DataGroup) object;
            DefaultMutableTreeNode childGroupNode = new DefaultMutableTreeNode(
                    new TreeNodeData(childGroup, childGroup.getAlias(), true), true);
            ((DefaultTreeModel) tree.getModel()).insertNodeInto(childGroupNode, parentNode, parentNode.getChildCount());

            //将数据项节点添加到数据组下
            List<DataItem> childItemList = new DataGroupManager4Postgresql().getDataItemsInGroup(childGroup.getName());
            for (DataItem childItem : childItemList) {
                DefaultMutableTreeNode childItemNode = new DefaultMutableTreeNode(
                        new TreeNodeData(childItem, childItem.getAlias(), true), false);
                ((DefaultTreeModel) tree.getModel()).insertNodeInto(childItemNode, childGroupNode, childGroupNode.getChildCount());
            }
        }
    }

    // 展开一个节点下的所有子节点
    public static void expandAllChildNodes(JTree tree, TreePath parentPath) {
        TreeNode parentNode = (TreeNode) parentPath.getLastPathComponent();
        if (parentNode.getChildCount() >= 0) {
            for (int i = 0; i < parentNode.getChildCount(); i++) {
                TreeNode childNode = parentNode.getChildAt(i);
                TreePath childPath = parentPath.pathByAddingChild(childNode);
                tree.expandPath(childPath);
                expandAllChildNodes(tree, childPath);
            }
        }
    }

    //构造一棵树并将所有节点展开显示
    public static void createTree(JTree tree) {
        DefaultMutableTreeNode root = (DefaultMutableTreeNode) tree.getModel().getRoot();
        createBranch(tree, root);
    }

    //构造一个树枝并将树枝的所有子节点展开显示
    public static void createBranch(JTree tree, DefaultMutableTreeNode branchRoot) {
        Queue<DefaultMutableTreeNode> queue = new LinkedList<>();
        queue.add(branchRoot);
        while (!queue.isEmpty()) {
            DefaultMutableTreeNode parentNode = queue.poll();
            TreeNodeData treeNodeData = (TreeNodeData) parentNode.getUserObject();
            Object object = treeNodeData.getRefObject();
            if (object instanceof DataCatalog) {
                DataCatalog parentCatalog = (DataCatalog) object;
                List<Object> childList = new DataCatalogManager4Postgresql().getChildren(parentCatalog.getName());
                for (Object o : childList) {
                    if (o instanceof DataCatalog) {
                        DataCatalog childCatalog = (DataCatalog) o;
                        DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(
                                new TreeNodeData(childCatalog, childCatalog.getAlias(), true), true);
                        ((DefaultTreeModel) tree.getModel()).insertNodeInto(childNode, parentNode, parentNode.getChildCount());
                        queue.add(childNode);
                    } else JTreeTools.expandGroup(tree, parentNode, o);
                }
            } else JTreeTools.expandGroup(tree, parentNode, object);
        }

        //让一个节点下的所有子节点展开显示
        TreePath parentPath = new TreePath(branchRoot.getPath());
        JTreeTools.expandAllChildNodes(tree, parentPath);

//        for (int i = 0; i < tree.getRowCount(); i++) {
//            tree.expandPath(tree.getPathForRow(i));
//        }
    }
}
